using CKS.AGV.Core;
using CKS.AGV.Core.Enum;
using FreeSql;
using FreeSql.DataAnnotations;

namespace CKS.Models
{
    /// <summary>
    /// agv任务表
    /// </summary>
    [Table(Name = "agv_convey_tasks")]
    public class agv_convey_tasks
    {
        private AgvTaskStatus taskStatus;

        /// <summary>
        /// 任务编号
        /// </summary>
        [Column(IsIdentity =true,IsPrimary = true)]
        public long Id { get; set; }
        /// <summary>
        /// 任务描述
        /// </summary>
        public string Desc { get; set; }
        /// <summary>
        /// 海康任务编号
        /// </summary>
        public string HikTaskCode { get; set; } = string.Empty;
        /// <summary>
        /// 任务类型：1 呼叫送料 2呼叫取料 3叉车任务
        /// </summary>
        public int TaskType {  get; set; }
        /// <summary>
        /// 是否手动测试，仅测试输送坐标
        /// </summary>
        public bool IsTest {  get; set; }
        /// <summary>
        /// 零件类型
        /// </summary>
        public int PartType {  get; set; }
        /// <summary>
        /// 路径起点
        /// </summary>
        public int StartPathPoint { get; set; }
        /// <summary>
        /// 路径终点
        /// </summary>
        public int EndPathPoint { get; set; }


        [Column(IsIgnore =true)]
        public string UniquePath
        {
            get
            {
                List<int> ints = new List<int>();
                ints.Add(StartPathPoint);
                ints.Add(EndPathPoint);
                ints = ints.OrderBy(a => a).ToList();
                return string.Join("|", ints);
            }
        }

        /// <summary>
        /// 起点请求信号
        /// </summary>
        public int StartReqSignalAddr { get; set; }
        /// <summary>
        /// 起点响应信号
        /// </summary>
        public int StartRspSignalAddr { get; set; }
        /// <summary>
        /// 终点请求信号
        /// </summary>
        public int EndReqSignalAddr { get; set; }
        /// <summary>
        /// 终点响应
        /// </summary>
        public int EndRspSignalAddr { get; set; }

        public int StartPlcId {  get; set; }

        public int EndPlcId { get; set; }

        /// <summary>
        /// 匹配出料成功
        /// </summary>
        public bool MatchOut {  get; set; }
        /// <summary>
        /// 任务状态
        /// </summary>
        [Column(MapType = typeof(int))]
        public AgvTaskStatus TaskStatus 
        { get => taskStatus;
            set {
                taskStatus = value;
               // OnPropertyChanged("TaskStatus");
            } 
        }
        /// <summary>
        /// AGV请求状态
        /// </summary>
        [Column(MapType = typeof(int))]
        public AgvStatus LastAgvReqStatus { get; set; }

        [Column(IsIgnore =true)]
        public bool CanSend
        {
            get
            {
                switch (TaskType)
                {
                    case 1:
                        if(MatchOut == true || AgvCache.AlwaysSend)
                            return true;
                        else
                            return false;
                    default:
                        return true;
                }
                
            }
        }
        /// <summary>
        /// 是否可以对取料点PLC回写状态
        /// </summary>
        [Column(IsIgnore = true)]
        public bool CanNotifyGet
        {
            get
            {
                return TaskType switch
                {
                    1 => false,
                    _ => true
                };
            }
        }
        /// <summary>
        /// 是否可以通知送料
        /// </summary>
        [Column(IsIgnore = true)]
        public bool CanNotifySend
        {
            get
            {
                return TaskType switch
                {
                    2 => false,
                    _ => true
                };
            }
        }

        /// <summary>
        /// 反馈AGV状态
        /// </summary>
        [Column(MapType = typeof(int))]
        public ResAgvStatus LastAgvRspStatus { get; set; }

        /// <summary>
        /// 下发任务时的二进制
        /// </summary>
        public string SendHex { get; set; } = string.Empty;
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedTime { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public bool IsDeleted { get; set; }
        /// <summary>
        /// 发送任务给AGV的时间
        /// </summary>
        public DateTime? SendTime { get;  set; }
        /// <summary>
        /// 0 默认 1需要取消 2取消完成
        /// </summary>
        public int CancelState { get; set; }
    }
}